<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace app\category\admin;

use app\admin\controller\Admin;
use app\category\model\Category as category;
use app\common\builder\ZBuilder;


class Index extends Admin
{
    function _initialize()
    {
        parent::_initialize();
        $_SESSION['sys_projectid'] = 66;
        $this->categoryModel = new category();
    }

    /**
     * @return mixed分类插件后台入口
     */
    function index()
    {
        $pluginCategoryModel = new category();
        $categoryTree = $pluginCategoryModel->adminCategoryTableTree();
        $this->assign('category_tree', $categoryTree);
        return $this->fetch('/admin_index');
    }

    function add()
    {
        $parentId = $this->request->param('parent', 0, 'intval');
        $options = $this->categoryModel->adminCategoryTree($parentId);
        return ZBuilder::make('form')
            ->setUrl(url('addPost'))
            ->addFormItems([ // 批量添加表单项
                ['select', 'parent_id', '上级分类', '', $options, $parentId],
                ['text', 'name', '分类名称', '必填，分类名称'],
                ['image', 'icon', '图标'],
                ['text', 'type', '类型'],
                ['text', 'description', '分类描述'],
                ['Number', '排序', '分类排序'],
                ['radio', 'status', '状态', '', [1 => '启用', 2 => '禁用'], 1]
            ])->fetch();
        //return ZBuilder::make('form')->addImage('post[icon]', '封面')->fetch('/add');
    }

    /**
     * 添加分类
     * @param title 标题
     * @param url   图片地址
     * @param status 状态 0显示 1不显示
     * @param type 类型 1网页 2手机
     * @param order 排序
     */
    function addPost()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $result = $this->validate($data, 'Form');
            if (true !== $result) $this->error($result);
            $data['projectid'] = $_SESSION['sys_projectid'];
            $data['createtime'] = date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']);
            $result = $this->categoryModel->addCategory($data);
            if ($result) {
                $this->success('添加成功!', url('index'));
            } else {
                $this->error('添加失败');
            }
        }
    }

    /**
     * 编辑分类
     * @param $id  分类id
     */
    function edit($id = null)
    {
        $id = $this->request->param('id', 0, 'intval');
        if ($id > 0) {
            $category = category::get($id)->toArray();
            $options = $this->categoryModel->adminCategoryTree($category['parent_id'], $id);
            return ZBuilder::make('form')
                ->setUrl(url('addPost'))
                ->addFormItems([ // 批量添加表单项
                    ['select', 'parent_id', '上级分类', '', $options, $category['parent_id']],
                    ['text', 'name', '分类名称', '必填，分类名称'],
                    ['text', 'id', '商品id', '', '', '', '', 'hidden'],
                    ['image', 'icon', '图标'],
                    ['text', 'type', '类型'],
                    ['text', 'description', '分类描述'],
                    ['Number', 'paixu', '分类排序'],
                    ['radio', 'status', '状态', '', [1 => '启用', 2 => '禁用'], 1]
                ])
                ->setUrl(url('editPost'))
                ->setFormData($category)
                ->fetch();
        } else {
            $this->error('操作错误!');
        }
    }

    /**
     * 编辑分类提交
     * @param
     */

    function editPost()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $result = $this->validate($data, 'Form');
            if (true !== $result) $this->error($result);
            $data['updatetime']=date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']);
            //项目id
            $where['projectid'] = $_SESSION['sys_projectid'];
            $where['id'] = $data['id'];
            $result = $this->categoryModel->saveData('category_category', $where, $data);
            if ($result) {
                $this->success('修改成功!', url('index'));
            } else {
                $this->error('修改失败');
            }
        }
    }

    /**
     * 分类删除
     * @param $id 分类id
     */

    function categoryDelete()
    {
        $id = $this->request->param('id');
        if (isset($id)) {
            //项目id
            $where['projectid'] = $_SESSION['sys_projectid'];
            $where['parent_id'] = $id;
            $result = $this->categoryModel->getListByWhere('category_category', $where, 'id', $order = 'id', $page_number = 1, $page_size = 10);
            if ($result) {
                $this->error('此分类有子类无法删除!');
            }
            $where1['projectid'] = $_SESSION['sys_projectid'];
            $where1['id'] = $id;
            $data['isdel'] = 1;
            $result1 = $this->categoryModel->saveData('category_category', $where1, $data);
            if ($result1) {
                $this->success("删除成功！", 'index');
            } else {

                $this->error('删除失败');
            }
        }
    }

    function get_categorys($parentId=0){
        $options = $this->categoryModel->adminCategoryTree($parentId);
        return $options;
    }
}
